package com.huoshan.im.server.utils;

import com.huoshan.im.server.config.ConfigHolder;
import org.springframework.util.CollectionUtils;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Set;

/**
 * @author: 火山九日
 * @since: 2020-06-24
 **/
public class ValidUtils {

    private final static Validator validator = ConfigHolder.getBean(Validator.class);

    /**
     * @param vo
     */
    public static String valid(Object vo) {
        Set<ConstraintViolation<Object>> constraintViolationSet = validator.validate(vo);

        if (!CollectionUtils.isEmpty(constraintViolationSet)) {
            StringBuilder message = new StringBuilder();
            for (ConstraintViolation<Object> objectConstraintViolation : constraintViolationSet) {
                message.append(objectConstraintViolation.getMessage()).append(",");
            }

            return message.toString();
        }
        return "";
    }

}
